<?php

namespace common\components\autoLoad;

use Yii;
use yii\log\Logger;
use yii\log\FileTarget;
use yii\helpers\VarDumper;

/**
 * Class Log
 * @package modules
 */
class Log
{

    /**
     * log
     * @param $level
     * @param string $message
     * @param null $data
     * @param null $logFile
     */
    protected static function log($level,$message='',$data=null,$logFile=null)
    {
        try {
            /** @var $log FileTarget */
            $log             = Yii::createObject([
                'class' => FileTarget::class,
                'logFile' => $logFile?:'@common/runtime/logs/modulesLog.log',
                'logVars' => [],
            ]);
            if($data){
                $message .= PHP_EOL.VarDumper::dumpAsString($data);
            }

            $log->messages[] = [PHP_EOL . $message . PHP_EOL, $level, null, time()];
            $log->export();
        }catch (\Exception $e){

        }
    }

    /**
     * error
     * @param string $message
     * @param null $data
     * @param null $logFile
     */
    public static function error($message='',$data=null,$logFile=null)
    {
        self::log(Logger::LEVEL_ERROR,$message,$data,$logFile);
    }

    /**
     * info
     * @param string $message
     * @param null $data
     * @param null $logFile
     */
    public static function info($message='',$data=null,$logFile=null)
    {
        self::log(Logger::LEVEL_INFO,$message,$data,$logFile);
    }

    /**
     * warning
     * @param string $message
     * @param null $data
     * @param null $logFile
     */
    public static function warning($message='',$data=null,$logFile=null)
    {
        self::log(Logger::LEVEL_WARNING,$message,$data,$logFile);
    }
}